<template>
  <div >
    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="primary"-->
<!--            plain-->
<!--            icon="Plus"-->
<!--            @click="handleAdd"-->
<!--            v-hasPermi="['scm:ScmInquiriesIn:add']"-->
<!--        >新增-->
<!--        </el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['scm:ScmInquiriesIn:edit']"
        >修改
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="danger"-->
<!--            plain-->
<!--            icon="Delete"-->
<!--            :disabled="multiple"-->
<!--            @click="handleDelete"-->
<!--            v-hasPermi="['scm:ScmInquiriesIn:remove']"-->
<!--        >删除-->
<!--        </el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Right"
            :disabled="multiple"
            @click="handleRdInquiry"
            v-hasPermi="['scm:ScmInquiriesIn:edit']"
        >RD询价
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="Download"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['scm:ScmInquiriesIn:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :search="false" :columns="columns" showColumnsType="transfer"
                     :linked-id="projectId" :attachment-type="1" :attachment-path="projectCode"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scmInquiriesInList" @selection-change="handleSelectionChange" style="width: 100%" :scrollbar-always-on="true" >
      <el-table-column type="selection" width="55" align="center" />
      <!-- 新增 scmProjectProducts 相关列 -->
      <el-table-column label="图号" align="center" prop="scmProjectProducts.drawingNumber" fixed width="98"/>
      <el-table-column label="版本" align="center" prop="scmProjectProducts.version" fixed v-if="columns[0].visible" />
      <el-table-column label="内版本" align="center" prop="scmProjectProducts.internalVersion" fixed v-if="columns[1].visible" />
      <el-table-column label="品名" align="center" prop="scmProjectProducts.productName" fixed v-if="columns[2].visible" />
<!--      <el-table-column label="" align="center" prop="id" />-->
<!--      <el-table-column label="询价单编号" align="center" prop="code" />-->
<!--      <el-table-column label="项目ID" align="center" prop="projectId" />-->
<!--      <el-table-column label="产品ID" align="center" prop="productId" />-->
      <el-table-column label="材质" align="center" prop="scmProjectProducts.material" v-if="columns[3].visible" />
      <el-table-column label="规格" align="center" prop="scmProjectProducts.specification" v-if="columns[4].visible" />
      <el-table-column label="颜色" align="center" prop="scmProjectProducts.color" v-if="columns[5].visible" />

        <el-table-column label="单台 用量" align="center" v-if="columns[6].visible" >
          <el-table-column label="数量" align="center" prop="onceQuantity" />
          <el-table-column label="单位" align="center" prop="onceUnit" />
        </el-table-column>
        <el-table-column label="月度 用量" align="center" v-if="columns[7].visible" >
          <el-table-column label="数量" align="center" prop="monthlyQuantity" />
          <el-table-column label="单位" align="center" prop="monthlyUnit" />
        </el-table-column>
        <el-table-column label="年度 用量" align="center" v-if="columns[8].visible" >
          <el-table-column label="年数量" align="center" prop="yearlyQuantity" />
          <el-table-column label="年单位" align="center" prop="yearlyUnit" />
        </el-table-column>

      <el-table-column label="报价条件" align="center">
        <el-table-column label="一次性" align="center" prop="disposableQuantity" v-if="columns[9].visible" />
        <el-table-column label="单位" align="center" prop="disposableUnit" v-if="columns[9].visible" />
        <el-table-column label="目标单价" align="center" prop="disposableUnitPrice" v-if="columns[9].visible" />
        <el-table-column label="希望纳期" align="center" prop="disposableExpectedDeliveryDate" v-if="columns[9].visible" width="110"/>
        <el-table-column label="一次性试做" align="center" prop="disposableTryQuantity" width="90" v-if="columns[10].visible" />
        <el-table-column label="单位" align="center" prop="disposableTryUnit" v-if="columns[10].visible" />
        <el-table-column label="目标单价" align="center" prop="disposableTryUnitPrice" v-if="columns[10].visible" />
        <el-table-column label="希望纳期" align="center" prop="disposableTryExpectedDeliveryDate" v-if="columns[10].visible" width="110"/>
        <el-table-column label="试做" align="center" prop="tryQuantity" v-if="columns[11].visible" />
        <el-table-column label="单位" align="center" prop="tryUnit" v-if="columns[11].visible" />
        <el-table-column label="目标单价" align="center" prop="tryUnitPrice" v-if="columns[11].visible" />
        <el-table-column label="希望纳期" align="center" prop="tryExpectedDeliveryDate" v-if="columns[11].visible" width="110"/>
        <el-table-column label="量产" align="center" prop="massQuantity" v-if="columns[12].visible" />
        <el-table-column label="单位" align="center" prop="massUnit" v-if="columns[12].visible" />
        <el-table-column label="目标单价" align="center" prop="massUnitPrice" v-if="columns[12].visible" />
        <el-table-column label="希望纳期" align="center" prop="massExpectedDeliveryDate" v-if="columns[12].visible" width="110"/>
      </el-table-column>

      <el-table-column label="希望报价日" align="center" prop="expectedQuotationDate" width="110" v-if="columns[13].visible"  >
        <template #default="scope">
          <!-- <span>{{ parseTime(scope.row.expectedQuotationDate, '{y}-{m}-{d}') }}</span> -->
          <span>{{ scope.row.expectedQuotationDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="希望到货日" align="center" prop="expectedDeliveryDate" width="110" v-if="columns[14].visible" >
        <template #default="scope">
          <!-- <span>{{ parseTime(scope.row.expectedDeliveryDate, '{y}-{m}-{d}') }}</span> -->
          <span>{{ scope.row.expectedDeliveryDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="需求资料" align="center" prop="expectedDatum" :show-overflow-tooltip="true" width="100" v-if="columns[15].visible" />
      <el-table-column label="备注" align="left" prop="remarks" :show-overflow-tooltip="true" width="100" v-if="columns[16].visible" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width " width="130">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['scm:ScmInquiriesIn:edit']">修改</el-button>
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['scm:ScmInquiriesIn:remove']">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>

  <!-- RD询价弹窗 -->
  <el-dialog
      v-model="dialogVisibleRd"
      title="选择供应商"
      width="600px"
  >

    <el-form-item label="供应商名称" prop="supplierId" style="width: 264.4px">
      <el-select
          v-model="selectedSuppliers" multiple filterable remote reserve-keyword placeholder="请输入简称"
          :remote-method="remoteMethodOfSupplier" @change="handleChangeOfSupplier" :loading="loading" >
        <el-option
            v-for="item in optionsOfSupplier" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancelRd">取消</el-button>
        <el-button type="primary" @click="handleConfirmRd">确认</el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 添加或修改客户询价单对话框 -->
  <el-dialog :title="title" v-model="open" width="1300px" append-to-body>
    <el-form ref="ScmInquiriesInRef" :model="form" :rules="rules" label-width="120px">
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="图号" prop="scmProjectProducts.drawingNumber">
            <el-input v-model="form.scmProjectProducts.drawingNumber" placeholder="请输入图号" readonly disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="品名" prop="scmProjectProducts.productName">
            <el-input v-model="form.scmProjectProducts.productName" placeholder="请输入品名" readonly disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="版本" prop="scmProjectProducts.version">
            <el-input v-model="form.scmProjectProducts.version" placeholder="请输入版本" readonly disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="内版本" prop="scmProjectProducts.internalVersion">
            <el-input v-model="form.scmProjectProducts.internalVersion" placeholder="请输入内版本" readonly/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="材质" prop="scmProjectProducts.material">
            <el-input v-model="form.scmProjectProducts.material" placeholder="请输入材质" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="规格型号" prop="specification">
            <el-input v-model="form.scmProjectProducts.specification" placeholder="请输入规格型号" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="颜色" prop="scmProjectProducts.color">
            <el-input v-model="form.scmProjectProducts.color" placeholder="请输入颜色" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="(单台用量)数量" prop="onceQuantity">
            <el-input-number v-model="form.onceQuantity" placeholder="请输入单台数量" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(单台用量)单位" prop="onceUnit">
            <el-input v-model="form.onceUnit" placeholder="请输入单台单位" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(月用量)数量" prop="monthlyQuantity">
            <el-input-number v-model="form.monthlyQuantity" placeholder="请输入月数量" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(月用量)单位" prop="monthlyUnit">
            <el-input v-model="form.monthlyUnit" placeholder="请输入月单位" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="(年用量)数量" prop="yearlyQuantity">
            <el-input-number v-model="form.yearlyQuantity" placeholder="请输入年数量" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(年用量)单位" prop="yearlyUnit">
            <el-input v-model="form.yearlyUnit" placeholder="请输入年单位" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
        </el-col>
        <el-col :span="6">
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="(一次性)数量" prop="disposableQuantity">
            <el-input-number v-model="form.disposableQuantity" placeholder="请输入一次性数量" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(一次性)单位" prop="disposableUnit">
            <el-input v-model="form.disposableUnit" placeholder="请输入一次性单位" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(一次性)目标单价" prop="disposableUnitPrice" label-width="160">
            <el-input-number v-model="form.disposableUnitPrice" placeholder="请输入一次性目标单价" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" :precision="2" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(一次性)希望纳期" prop="disposableExpectedDeliveryDate" label-width="160">
            <el-date-picker clearable v-model="form.disposableExpectedDeliveryDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择一次性希望纳期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="(一次性试做)数量" prop="disposableTryQuantity">
            <el-input-number v-model="form.disposableTryQuantity" placeholder="请输入一次性试做数量" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(一次性试做)单位" prop="disposableTryUnit">
            <el-input v-model="form.disposableTryUnit" placeholder="请输入一次性试做单位" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(一次性试做)目标单价" prop="disposableTryUnitPrice" label-width="160">
            <el-input-number v-model="form.disposableTryUnitPrice" placeholder="请输入一次性试做目标单价" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" :precision="2" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(一次性试做)希望纳期" prop="disposableTryExpectedDeliveryDate" label-width="160">
            <el-date-picker clearable v-model="form.disposableTryExpectedDeliveryDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择一次性试做希望纳期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="(试做)数量" prop="tryQuantity">
            <el-input-number v-model="form.tryQuantity" placeholder="请输入试做数量" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(试做)单位" prop="tryUnit">
            <el-input v-model="form.tryUnit" placeholder="请输入试做单位" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(试做)目标单价" prop="tryUnitPrice" label-width="160">
            <el-input-number v-model="form.tryUnitPrice" placeholder="请输入试做目标单价"  type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" :precision="2" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(试做)希望纳期" prop="tryExpectedDeliveryDate" label-width="160">
            <el-date-picker clearable v-model="form.tryExpectedDeliveryDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择试做希望纳期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="(量产)数量" prop="massQuantity">
            <el-input-number v-model="form.massQuantity" placeholder="请输入量产数量" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(量产)单位" prop="massUnit" label-width="160">
            <el-input v-model="form.massUnit" placeholder="请输入量产单位" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(量产)目标单价" prop="massUnitPrice" label-width="160">
            <el-input-number v-model="form.massUnitPrice" placeholder="请输入量产目标单价" type="number" :min="0" :controls="false" class="number-right" style="width: 100%;" :precision="2" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="(量产)希望纳期" prop="massExpectedDeliveryDate" label-width="160">
            <el-date-picker clearable v-model="form.massExpectedDeliveryDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择量产希望纳期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="希望报价日" prop="expectedQuotationDate">
            <el-date-picker clearable v-model="form.expectedQuotationDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择希望报价日">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="希望到货日" prop="expectedDeliveryDate">
            <el-date-picker clearable v-model="form.expectedDeliveryDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择希望到货日">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="需求资料" prop="expectedDatum">
            <el-input v-model="form.expectedDatum" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="备注" prop="remarks">
            <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-col>
      </el-row>



<!--      <el-form-item label="询价单编号" prop="code">-->
<!--        <el-input v-model="form.code" placeholder="请输入询价单编号" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="项目ID" prop="projectId">-->
<!--        <el-input v-model="form.projectId" placeholder="请输入项目ID" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="产品ID" prop="productId">-->
<!--        <el-input v-model="form.productId" placeholder="请输入产品ID" />-->
<!--      </el-form-item>-->

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>

</template>

<script setup>
import { ref, onMounted } from 'vue'
import { listScmInquiriesIn, getScmInquiriesIn, delScmInquiriesIn, addScmInquiriesIn, updateScmInquiriesIn } from "@/api/scm/ScmInquiriesIn";
import { listSupplier } from "@/api/scm/supplier";
import {updateProjects} from "@/api/scm/projects.js";
import {createScmInquiriesOut} from "@/api/scm/scmInquiriesOut.js";
// import { parseTime } from '@/utils/dateUtils'
const { proxy } = getCurrentInstance();
const props = defineProps({ 
  projectId: { 
    type: [String, Number],
    default: '' 
  },
  projectCode: {
    type: String,
    default: ''
  }
})

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    code: null,
    projectId: props.projectId,
    productId: null,
    expectedQuotationDate: null,
    expectedDeliveryDate: null,
    expectedDatum: null,
  },
  rules: {
    projectId: [
      { required: true, message: "项目ID不能为空", trigger: "blur" }
    ],
    productId: [
      { required: true, message: "产品ID不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

// const localProjectId = ref(props.projectId);
// 监听 projectId 变化
watch(
    () => props.projectId,
    (newVal) => {
      console.log('projectId changed to:', newVal);
      queryParams.value.projectId = newVal; // 更新本地变量
      getList();
    }
);

const showSearch = ref(true)
const loading = ref(false)
const scmInquiriesInList = ref([])
const total = ref(0)
const single = ref(true)
const multiple = ref(true)
const title = ref('')
const open = ref(false)
const ids = ref([]);

// const handleQuery = () => {
//   getList()
// }

const resetQuery = () => {
  queryParams.value = {
    code: '',
    projectId: props.projectId,
    productId: '',
    expectedQuotationDate: '',
    expectedDeliveryDate: '',
    pageNum: 1,
    pageSize: 10
  }
  getList()
}


// 多选框选中数据
const handleSelectionChange = (selection) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
  console.log("ids:" + ids.value);
}

const getList = () => {
  loading.value = true
  queryParams.value.projectId = props.projectId
  listScmInquiriesIn(queryParams.value).then(response => {
    scmInquiriesInList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  })
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    code: null,
    projectId: null,
    productId: null,
    material: null,
    specification: null,
    color: null,
    onceQuantity: null,
    onceUnit: null,
    monthlyQuantity: null,
    monthlyUnit: null,
    yearlyQuantity: null,
    yearlyUnit: null,
    disposableQuantity: null,
    disposableUnit: null,
    disposableUnitPrice: 0 ,
    disposableExpectedDeliveryDate: null,
    disposableTryQuantity: null,
    disposableTryUnit: null,
    disposableTryUnitPrice: 0,
    disposableTryExpectedDeliveryDate: null,
    tryQuantity: null,
    tryUnit: null,
    tryUnitPrice: 0,
    tryExpectedDeliveryDate: null,
    massQuantity: null,
    massUnit: null,
    massUnitPrice: 0,
    massExpectedDeliveryDate: null,
    expectedQuotationDate: null,
    expectedDeliveryDate: null,
    expectedDatum: null,
    remarks: null,
    scmProjectProducts: {
      drawingNumber: null,
      version: null,
      internalVersion: null,
      productName: null,
      material: null,
      inflamingRetardingLevel: null,
      specification: null,
      model: null,
      color: null,
      drawingComposition: null,
    },
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("ScmInquiriesInRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
// function resetQuery() {
//   proxy.resetForm("queryRef");
//   handleQuery();
// }

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加客户询价单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getScmInquiriesIn(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改客户询价单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["ScmInquiriesInRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateScmInquiriesIn(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addScmInquiriesIn(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除客户询价单编号为"' + _ids + '"的数据项？').then(function() {
    return delScmInquiriesIn(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}



onMounted(() => {
  getList()
})

//RD询价开始
// 弹窗控制
const dialogVisibleRd = ref(false)
const supplierList = ref([])
const selectedSuppliers = ref([])

// 加载供应商数据
const loadSuppliers = () => {
  listSupplier().then(response => {
    supplierList.value = response.rows || response.data || []
  })
}

// 点击“RD询价”按钮触发
const handleRdInquiry = () => {
  // selectedSuppliers.value = []
  loadSuppliers()
  dialogVisibleRd.value = true
}

const formRd = ref({});
// 确认按钮
const handleConfirmRd = () => {
  console.log('选中的供应商:', selectedSuppliers.value)
  console.log("ids:" + ids.value);

  const supplierNames = selectedSuppliers.value.map(id => {
    const supplier = supplierList.value.find(item => item.id === id);
    return supplier ? supplier.nickname : null; // 或者 .nickname 依据你的字段
  }); // 过滤掉找不到的 id 对应的 null  .filter(name => name !== null)
  console.log('选中的供应商 Names:', supplierNames);

  formRd.value = {
    ids: ids.value,
    supplierIds: selectedSuppliers.value,
    supplierNames: supplierNames,
  }

  console.log("form:", formRd);
  createScmInquiriesOut(formRd.value).then(response => {
    proxy.$modal.msgSuccess("添加成功");
  });
  // 这里可以执行你导出或提交的逻辑
  dialogVisibleRd.value = false
}

// 取消按钮
const handleCancelRd = () => {
  dialogVisibleRd.value = false
}

// 多选事件
const handleSelectionChangeRd = (val) => {
  selectedSuppliers.value = val
}

// 供应商下拉框
const optionsOfSupplier = ref([]);
// const supplierList = ref([]);
const queryParamsOfSupplier = ref({});
const selectedSupplierIds = ref([]);

function resetQueryOfSupplier() {
  queryParamsOfSupplier.value = {
    pageNum: 1,
    pageSize: 100,
    id: null,
    supplierNo: null,
    name: null,
    nickname: null,
  }
}
function remoteMethodOfSupplier(query) {
  console.log('query', query);
  resetQueryOfSupplier();
  if (query !== ''){
    queryParamsOfSupplier.value.nickname = query.trim();
    listSupplierSelect();
  }
}
function initializeSupplierOptions(supplierId) {
  console.log('supplierId', supplierId)
  resetQueryOfSupplier();
  queryParamsOfSupplier.value.id = supplierId;
  listSupplierSelect();
  queryParamsOfSupplierContacts.value.supplierId = supplierId;
  listSupplierContactsSelect();
}
function handleChangeOfSupplier(value) {
  console.log('value', value);
  selectedSupplierIds.value = value;
  console.log('selectedSupplierIds', selectedSupplierIds.value);
  // form.value.supplierId = value;
  // form.value.supplierTitle = supplierList.value.find(supplier => supplier.id === value).nickname;
  // form.value.supplierContactId = 0;
  // form.value.supplierContactName = " ";

  // queryParamsOfSupplierContacts.value.supplierId = value;
  // listSupplierContactsSelect();
}
function listSupplierSelect() {
  listSupplier(queryParamsOfSupplier.value).then(response => {
    supplierList.value = response.rows;
    optionsOfSupplier.value = supplierList.value.map(supplier => ({
      label: supplier.name,
      value: supplier.id,
    }));
  });
}
//RD询价结束

// 列显隐信息
const columns = ref([
  { key: 0, label: `版本`, visible: true },
  { key: 1, label: `内版本`, visible: true },
  { key: 2, label: `品名`, visible: true },
  { key: 3, label: `材质`, visible: true },
  { key: 4, label: `规格`, visible: true },
  { key: 5, label: `颜色`, visible: true },
  { key: 6, label: `单台 用量`, visible: true },
  { key: 7, label: `月度 用量`, visible: true },
  { key: 8, label: `年度 用量`, visible: true },
  { key: 9, label: `一次性`, visible: true },
  { key: 10, label: `一次性试做`, visible: true },
  { key: 11, label: `试做`, visible: true },
  { key: 12, label: `量产`, visible: true },
  { key: 13, label: `希望报价日`, visible: true },
  { key: 14, label: `希望到货日`, visible: true },
  { key: 15, label: `需求资料`, visible: true },
  { key: 16, label: `备注`, visible: true }
]);

</script>

<style scoped>
.app-container {
  padding: 20px;
}

</style>